package com.xingda.packing.domain;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xingda.packing.enums.AlarmTriggerType;
import com.xingda.packing.events.EventCreater;
import com.xingda.packing.minitor.entity.PkAlarmType;
import com.xingda.packing.minitor.entity.PkPlcAlarm;
import com.xingda.packing.minitor.mapper.PkAlarmTypeMapper;
import com.xingda.packing.minitor.mapper.PkPlcAlarmMapper;
import com.xingda.packing.service.TaskManagerService1;
import com.xingda.packing.service.TaskManagerService2;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
 * 打包生产线
 */
@Component
@Slf4j
public class PackProductionLine {
    @Value("${spring.profiles.active}")
    @Getter
    private String env;
    @Value("${com.xingda.model}")
    @Getter
    private String model;
    @Value("${com.xingda.area}")
    @Getter
    private String area;
    @Value("${com.xingda.region}")
    @Getter
    private String region;
    @Getter
    @Resource
    private EventCreater eventCreater;
    @Resource
    private PkAlarmTypeMapper pkAlarmTypeMapper;
    @Resource
    private PkPlcAlarmMapper pkPlcAlarmMapper;
    @Resource
    private TaskManagerService1 taskManagerService1;
    @Resource
    private TaskManagerService2 taskManagerService2;
    @Getter
    @Setter
    private boolean initRunning1 = false;
    @Getter
    @Setter
    private boolean initRunning2 = false;
    @Getter
    @Setter
    private boolean initRunning3 = false;
    /**
     * 机器人
     */
    @Getter
    @Resource
    private Robot robot;
    /**
     * 缓存区
     */
    @Getter
    @Resource
    private CacheArea cacheArea;
    /**
     * 产品包装区
     */
    @Getter
    @Resource
    private ProductArea productArea;

    public boolean initRunning(int area) {
        if (area == 1) {
            return initRunning1;
        } else if (area == 2) {
            return initRunning2;
        } else if (area == 3) {
            return initRunning3;
        }

        log.error("PackProductionLine的initRunning参数area错误，无法判断是否初始化");
        return false;
    }

    public void setInitRunning(int area, boolean value) {
        if (area == 1) {
            initRunning1 = value;
        } else if (area == 2) {
            initRunning2 = value;
        } else if (area == 3) {
            initRunning3 = value;
        }
    }

    public void start(int area) {
        this.robot.resetGrabState(area);
        this.robot.resetPalletizingState(area);
        if (area == 1) {
            CompletableFuture.runAsync(taskManagerService1);
        } else {
            CompletableFuture.runAsync(taskManagerService2);
        }
    }

    public void initStatus(int area) {
        // 加载装箱位数据
        this.getProductArea().initStatus(area);
        // 加载缓存位数据
        this.getCacheArea().initStatus(area);
        // 初始化机器人状态
        this.getRobot().initStatus(area);
    }

    /**
     * 调度中的报警(缓存位满、装箱位满等)
     */
    public void triggerAlarm(int area) {
        this.getProductArea().triggerFullAlarm(area);
    }

    public void initAlarmType() {
        //加载PLC报警类型
        boolean env = getEnv().startsWith("xd-prod");
        if (env) {
            LambdaQueryWrapper<PkPlcAlarm> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.isNull(PkPlcAlarm::getAlarmTypeId);
            List<PkPlcAlarm> pkPlcAlarms = pkPlcAlarmMapper.selectList(lambdaQueryWrapper);
            if (!pkPlcAlarms.isEmpty()) {
                for (PkPlcAlarm pkPlcAlarm : pkPlcAlarms) {
                    if (Objects.isNull(pkPlcAlarm.getAlarmTypeId())) {
                        PkAlarmType pkAlarmType = new PkAlarmType();
                        pkAlarmType.setAlarmAreaId(-1);
                        pkAlarmType.setAddressIndex(pkPlcAlarm.getId() + 100);
                        pkAlarmType.setContent(pkPlcAlarm.getContent());
                        pkAlarmType.setClassify("PLC");
                        pkAlarmType.setShow(true);
                        pkAlarmType.setTriggerCondition(true);
                        pkAlarmType.setSend(true);
                        pkAlarmType.setRelieveNotice(true);
                        pkAlarmTypeMapper.insert(pkAlarmType);
                        pkPlcAlarm.setAlarmTypeId(pkAlarmType.getId());
                        pkPlcAlarmMapper.updateById(pkPlcAlarm);
                    }
                }
            }
        }
        //加载MES报警类型
        for (AlarmTriggerType alarmTriggerType : AlarmTriggerType.values()) {
            LambdaQueryWrapper<PkAlarmType> pkAlarmTypeQueryWrapper = Wrappers.lambdaQuery();
            pkAlarmTypeQueryWrapper.eq(PkAlarmType::getAddressIndex, alarmTriggerType.getAddressIndex());
            PkAlarmType pk = pkAlarmTypeMapper.selectOne(pkAlarmTypeQueryWrapper);
            if (Objects.isNull(pk)) {
                PkAlarmType pkAlarmType = new PkAlarmType();
                pkAlarmType.setAlarmAreaId(alarmTriggerType.getAreaId());
                pkAlarmType.setAddressIndex(alarmTriggerType.getAddressIndex());
                pkAlarmType.setContent(alarmTriggerType.getContent());
                pkAlarmType.setClassify(alarmTriggerType.getClassify());
                pkAlarmType.setShow(alarmTriggerType.isShow());
                pkAlarmType.setTriggerCondition(alarmTriggerType.isTriggerCondition());
                pkAlarmType.setSend(alarmTriggerType.isSend());
                pkAlarmType.setRelieveNotice(alarmTriggerType.isRelieveNotice());
                pkAlarmTypeMapper.insert(pkAlarmType);
            } else {
                pk.setContent(alarmTriggerType.getContent());
                pkAlarmTypeMapper.updateById(pk);
            }
        }
    }
}
